-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
SKILL_D = {}

---! 查找技能对象
function SKILL_D:find_skill(skillid, skilllev)
    local configs = IMPORT_D:query("skilllev")
    if not configs then
        return
    end

    local config = configs[skillid]
    if not config then
        return
    end

    local skill = config[skilllev or 1]
    if not skill then
        return
    end

    return skill
end

---! 改变技能
function SKILL_D:change_skill(player, skillid)
    local skilllev = player:get_skill_level(skillid)
    if not skilllev then
        local result = {}
        result.code = 401
        player:send_packet("MSGS2CChangeSkill", result)
        return
    end

    ---! 修改当前玩家技能
    player:set_use_skill(skillid)

    local result = {}
    result.code = 200
    result.skillid = skillid
    player:send_packet("MSGS2CChangeSkill", result)
end

---! 提升技能
function SKILL_D:improve_skill_level(player, skillid)
    local skilllev = player:get_skill_level(skillid)
    if not skilllev then
        ---! 尚未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CSkillLevelup", result)
        return
    end

    local skill = SKILL_D:find_skill(skillid, skilllev)
    if not skill then
        ---! 技能配置不存在
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CSkillLevelup", result)
        return
    end

    local next_skill = SKILL_D:find_skill(skillid, skill.lv + 1)
    if not next_skill then
        ---! 当前技能已满级
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CSkillLevelup", result)
        return
    end

    if skill.levtype == 2 and player:get_stage_level() < skill.levtypenum then
        ---! 尚未通过关卡
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CSkillLevelup", result)
        return
    end

    if player:get_currency(GamePropIds.kGamePropIdsDiamondsIcon) < skill.cost then
        ---! 所需元宝不足
        local result = {}
        result.error = 5
        player:send_packet("MSGS2CSkillLevelup", result)
        return
    end

    ---! 扣除所需元宝
    player:cost_currency(GamePropIds.kGamePropIdsDiamondsIcon, skill.cost)

    ---! 提升当前技能
    player:set_skill_level(skillid, next_skill.lv)

    ---! 通知提升成功
    local result = {}
    result.error = 0
    player:send_packet("MSGS2CSkillLevelup", result)

    ---! 刷新元宝数量
    ITEM_D:send_currency_info(player)

    ---! 刷新技能列表
    SKILL_D:send_skill_info(player)

    ---! 同步技能排名
    RANK_D:sync_rank_data(player, "skills_rank")
end

---! 解锁技能
function SKILL_D:unlock_skill(player, skillid)
    local skill = SKILL_D:find_skill(skillid)
    if not skill then
        ---! 技能配置不存在
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CSkillUnlock", result)
        return
    end

    local skilllev = player:get_skill_level(skill.skilltype)
    if skilllev then
        ---! 已经解锁
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CSkillUnlock", result)
        return
    end

    if skill.type == 2 and player:get_stage_level() < skill.levtypenum then
        ---! 尚未通过关卡
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CSkillUnlock", result)
        return
    end

    if skill.type == 3 and player:get_currency(GamePropIds.kGamePropIdsDiamondsIcon) < skill.typenum then
        ---! 需要元宝不足
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CSkillUnlock", result)
        return
    end

    if skill.type == 3 then
        ---! 扣除响应元宝
        player:cost_currency(GamePropIds.kGamePropIdsDiamondsIcon, skill.typenum)

        ---! 刷新元宝数量
        ITEM_D:send_currency_info(player)
    end

    ---! 解锁当前技能
    player:set_skill_level(skillid, 1)

    ---! 通知解锁成功
    local result = {}
    result.error = 0
    player:send_packet("MSGS2CSkillUnlock", result)

    ---! 刷新技能列表
    SKILL_D:send_skill_info(player)
end

---! 初始技能列表
function SKILL_D:init_skill_info(player)
    local configs = IMPORT_D:query("skilllev")
    if not configs then
        return
    end

    for skillid, config in ipairs(configs) do
        local skill = config[1]
        if skill and skill.type == 1 then
            ---! 初始获得的技能
            player:set_skill_level(skillid, skill.lv)
        end
    end
end

---! 刷新技能列表
function SKILL_D:send_skill_info(player)
    local skills = {}
    local skill_tbl = player:query("skills") or {}
    for skillid, level in pairs(skill_tbl) do
        table.insert(skills, {
            id = tonumber(skillid),
            level = level,
        })
    end

    local result = {}
    result.skills = skills
    player:send_packet("MSGS2CSkillList", result)
end
